//
//  RegisterViewController.m
//  XMPPHelper
//
//  Created by 蔡成汉 on 15/5/13.
//  Copyright (c) 2015年 JW. All rights reserved.
//

#import "RegisterViewController.h"
#import "XMPPHelper.h"

@interface RegisterViewController ()<UITextFieldDelegate>
{
    UITextField *userNameTextField;
    UITextField *userPasTextField;
    UIButton *registerUserButton;
}
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册新用户";
    
    userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 100, 200, 30)];
    userNameTextField.delegate = self;
    userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    userNameTextField.placeholder = @"用户名";
    userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    userNameTextField.returnKeyType = UIReturnKeyNext;
    [userNameTextField becomeFirstResponder];
    userNameTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.view addSubview:userNameTextField];
    
    userPasTextField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 150, 200, 30)];
    userPasTextField.delegate = self;
    userPasTextField.borderStyle = UITextBorderStyleRoundedRect;
    userPasTextField.placeholder = @"密码";
    userPasTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    userPasTextField.clearsOnBeginEditing = YES;
    userPasTextField.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:userPasTextField];
    
    registerUserButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerUserButton.frame = CGRectMake((self.view.frame.size.width/2 - 100), 200, 200, 30);
    [registerUserButton setTitle:@"注册新用户" forState:UIControlStateNormal];
    [registerUserButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [registerUserButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [registerUserButton addTarget:self action:@selector(registerUserButtonTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerUserButton];

}

-(void)registerUserButtonTouch:(UIButton *)paramSender
{
    XMPPHelper *myXMPPHelper = [XMPPHelper myXMPPHelper];
    [myXMPPHelper registerXMPPUser:userNameTextField.text psd:userPasTextField.text.md5 result:^(BOOL registerResult, NSError *error) {
        if (registerResult == YES)
        {
            NSLog(@"注册成功");
        }
        else
        {
            NSLog(@"注册失败");
        }
    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == userNameTextField)
    {
        [textField resignFirstResponder];
        [userPasTextField becomeFirstResponder];
    }
    else
    {
        [textField resignFirstResponder];
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
